package com.ks.comment.service.impl;

import com.baomidou.mybatisplus.mapper.EntityWrapper;
import com.ks.comment.controller.dto.JournalResponse;
import com.ks.comment.entity.ZpzJournal;
import com.ks.comment.dao.ZpzJournalMapper;
import com.ks.comment.service.IZpzJournalService;
import com.baomidou.mybatisplus.service.impl.ServiceImpl;
import com.ks.comment.service._ServiceBase;
import com.ks.core.util.ToolUtil;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * <p>
 * 信息收藏日志 服务实现类
 * </p>
 *
 * @author kingdee123
 * @since 2020-12-18
 */
@Service
public class ZpzJournalServiceImpl extends _ServiceBase<ZpzJournalMapper, ZpzJournal> implements IZpzJournalService {
    @Resource
    private ZpzJournalMapper zpzJournalMapper;


    @Override
    public int queryJournalStatus(String token, int informationId, String type) {
        String userId = this.getUserId(token);
        if (ToolUtil.isNotEmpty(userId)) {
            ZpzJournal zpzJournal = new ZpzJournal();
            zpzJournal.setIuserId(Integer.valueOf(userId));
            zpzJournal.setInformationId(informationId);
            zpzJournal.setCType(type);
            ZpzJournal journal = zpzJournalMapper.selectOne(zpzJournal);
            if (ToolUtil.isNotEmpty(journal)) {
                return 1;
            }
        }
        return 0;
    }

    @Override
    public int addJournal(String token, int informationId, String type) {
        String userId = this.getUserId(token);
        if (ToolUtil.isNotEmpty(userId)) {
            ZpzJournal zpzJournal = new ZpzJournal();
            zpzJournal.setIuserId(Integer.valueOf(userId));
            zpzJournal.setInformationId(informationId);
            zpzJournal.setCType(type);
            Integer insert = zpzJournalMapper.insert(zpzJournal);
            if (insert > 0) {
                return insert;
            }
        }
        return 0;
    }

    @Override
    public Map<String, Object> getJournalList(int informationId, String type, int pageNo, int pageSize) {
        Map<String, Object> map = new HashMap<>();
        List<JournalResponse> journalList = zpzJournalMapper.getJournalList(informationId, type, pageNo, pageSize);
        int journalCount = zpzJournalMapper.getJournalCount(informationId, type);
        map.put("list", journalList);
        map.put("count", journalCount);
        return map;
    }
}
